package com.anuo.app.modules.system.service;

import com.anuo.app.common.basedata.BusinessType;
import com.anuo.app.common.basedata.PayDic;
import com.anuo.app.common.globalhandle.AppException;
import com.anuo.app.common.globalhandle.GlobalConfig;
import com.anuo.app.common.thirdparty.pay.AlipayImpl;
import com.anuo.app.common.thirdparty.pay.Pay;
import com.anuo.app.common.thirdparty.pay.WeChatPayImpl;
import com.anuo.app.common.utils.DateUtil;
import com.anuo.app.common.utils.IdUtil;
import com.anuo.app.common.utils.StringUtil;
import com.anuo.app.modules.coach.service.CoachService;
import com.anuo.app.modules.system.dao.SystemDao;
import com.anuo.app.modules.system.entity.*;
import javafx.scene.effect.MotionBlur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class SystemService {
    @Autowired
    SystemDao systemDao;
    @Autowired
    CoachService coachService;

    @Transactional(readOnly = false)
    public void insertMobileCode(MobileCode mobileCode) {
        MobileCode mobileCodeDb = systemDao.getMobileCode(mobileCode);
        String duration = GlobalConfig.getConfig("mobile.code.duration");
        mobileCode.setExpiredtime(DateUtil.addMinutes(new Date(), Integer.parseInt(duration)));
        if (mobileCodeDb != null) {
            systemDao.updateMobileCode(mobileCode);
        } else {
            mobileCode.preInsert();
            systemDao.insertMobileCode(mobileCode);
        }

    }

    public MobileCode getMobileCode(SystemFilter systemFilter) {
        MobileCode mobileCode = new MobileCode();
        mobileCode.setMobile(systemFilter.getMobile());
        return systemDao.getMobileCode(mobileCode);
    }

    @Transactional(readOnly = false)
    public void saveSystemPushid(SystemPushid systemPushid) {
        SystemFilter systemFilter = new SystemFilter();
        systemFilter.setUserid(systemPushid.getUserid());
        SystemPushid dbSystemPushid = systemDao.getSystemPushid(systemFilter);
        if (dbSystemPushid == null) {
            systemPushid.preInsert();
            systemDao.insertSystemPushid(systemPushid);
        } else {
            systemPushid.setUpdatedate(new Date());
            systemDao.updateSystemPushid(systemPushid);
        }
    }

    @Transactional(readOnly = true)
    public SystemPushid getSystemPushid(SystemFilter systemFilter) {
        return systemDao.getSystemPushid(systemFilter);
    }

    @Transactional(readOnly = false)
    public String saveVipBill(VipBill vipBill) {
        if (StringUtil.isEmpty(vipBill.getId())) {
            vipBill.preInsert();
            String orderNo = IdUtil.INSTANCE.getSequenceId();
            vipBill.setOrderno(orderNo);
            systemDao.insertVipBill(vipBill);
            return orderNo;
        }
        return "";
    }

    @Transactional(readOnly = true)
    public VipBill getVipBill(SystemFilter systemFilter) {
        return systemDao.getVipBill(systemFilter);
    }

    @Transactional(readOnly = true)
    public String getPayString(SystemFilter systemFilter, String clientIp) {
        VipBill vipBill = systemDao.getVipBill(systemFilter);
        if (vipBill == null) {
            throw new AppException("");
        }
        PayInfo payInfo = new PayInfo();
        payInfo.setPay_remark(vipBill.getPay_remark());
        payInfo.setOrderno(vipBill.getOrderno());
        payInfo.setAmount(vipBill.getAmount().toString());
        payInfo.setClientip(clientIp);
        Pay pay = getPayInstance(systemFilter.getPay_type());
        return pay.getPayString(payInfo);
    }

    private Pay getPayInstance(String payType) {
        Pay pay = null;
        if (PayDic.ALIPAY.equals(payType)) {
            pay = new AlipayImpl();
        } else if (PayDic.WECHAT_PAY.equals(payType)) {
            pay = new WeChatPayImpl();
        }
        return pay;
    }

    @Transactional(readOnly = false)
    public void saveVipPaid(VipBill vipBillParam) {
        //获取订单
        SystemFilter systemFilter = new SystemFilter();
        systemFilter.setOrderno(vipBillParam.getOrderno());
        VipBill vipBill = systemDao.getVipBill(systemFilter);
        //修改订单为已支付
        vipBill.setPay_state(PayDic.PAID);
        systemDao.updateVipBillForPaid(vipBill);
        //执行其他业务逻辑
        switch (vipBill.getBusiness_type()) {
            case BusinessType.COACH:
                dealCoachPay(vipBill);//处理教练相关的业务
        }
    }

    /**
     * 处理教练相关的支付业务
     *
     * @param vipBill
     */
    @Transactional(readOnly = false)
    private void dealCoachPay(VipBill vipBill) {
        //将待支付的记录插入到正式表
        coachService.insertCoachMoneyFromNotPay(vipBill.getBusiness_id());
        //修改购买记录为正式记录
        coachService.updateCoachStudentMoneyToFormal(vipBill.getBusiness_id());
        //删除待支付的记录
        coachService.deleteCoachStudentMoneyNotPay(vipBill.getBusiness_id());
    }


    /**
     * 手机验证码是否过期
     * @param mobileCode
     * @return
     */
    public boolean isMobileCodeExipre(MobileCode mobileCode) {
        MobileCode mobileCode_db = systemDao.getMobileCode(mobileCode);
        if (mobileCode_db != null) {
            Date currentDateTime = new Date();
            if (currentDateTime.after(mobileCode.getExpiredtime())) {
                return true;
            }
        }
        else{
            return true;
        }

        return false;
    }

    /**
     * 检查验证码是否有效
     * @param moblie
     * @param code
     */
    public void checkMobileCode(String moblie,String code){
        SystemFilter systemFilter = new SystemFilter();
        systemFilter.setMobile(moblie);
        MobileCode mobileCode = getMobileCode(systemFilter);
        if (mobileCode == null) {
            throw new AppException("此手机号没有验证码,请获取");
        }
        if (!mobileCode.getCode().equals(code)) {
            throw new AppException("手机验证码错误");
        }
        //判断验证码是否过期
        if (isMobileCodeExipre(mobileCode)) {
            throw new AppException("手机验证码已过期, 请重新获取");
        }
    }


    public Appversion getAppversion(){
        return systemDao.getAppversion();
    }

    public List<City> getCityList(SystemFilter systemFilter){
        return systemDao.getCityList(systemFilter);
    }

}
